<?php
class Task {
    var $id;
    var $username;
    var $descripcion;
    var $fecha;
    var $duracion;
    var $anterior;
    var $siguiente;
    
    function Task(){
        date_default_timezone_set('America/Santiago');  
        $foo = new DateTime("now");
        $this->fecha = $foo->format('Y-m-d H:i');
    }
    
    function get(){
        $query ="select * from task where id = '$this->id';";
        $result = mysql_query($query);
        $row = mysql_fetch_array($result);
        $this->id = $row['id'];
        $this->username = $row['username'];
        $this->descripcion = $row['descripcion'];
        $this->fecha = $row['fecha'];
        $this->duracion = $row['duracion'];
    }
    
    function getAnterior(){
        $query ="select * from task where fecha < '$this->fecha' and username = '$this->username' order by fecha DESC limit 1;";
        $result = mysql_query($query);
        $row = mysql_fetch_array($result);
        $this->anterior=new Task();
        $this->anterior->id = $row['id'];
        $this->anterior->username = $row['username'];
        $this->anterior->descripcion = $row['descripcion'];
        $this->anterior->fecha = $row['fecha'];
        $this->anterior->duracion = $row['duracion'];
        return $this->anterior; 
    }
    
    function getSiguiente(){
        $query ="select * from task where fecha > '$this->fecha' and username = '$this->username' order by fecha DESC limit 1;";
        $result = mysql_query($query);
        $row = mysql_fetch_array($result);
        $this->siguiente=new Task();
        $this->siguiente->id = $row['id'];
        $this->siguiente->username = $row['username'];
        $this->siguiente->descripcion = $row['descripcion'];
        $this->siguiente->fecha = $row['fecha'];
        $this->siguiente->duracion = $row['duracion'];
        return $this->siguiente;
    }

    function save() {
        $descrip = htmlentities($this->descripcion);
        $query_insert =" INSERT INTO task (username, fecha ,descripcion) VALUES ('$this->username', NOW( ) , '$descrip');";
        $result = mysql_query($query_insert);
        $this->id = mysql_insert_id();
        $this->get();
        if ($this->anterior==null)
            $this->getAnterior();
        $this->anterior->updateDuracion();
    }
    
    function updateDuracion() {
    
    
        /*$query ="select * from task where username = '$this->username'  and id> $this->id order by fecha DESC limit 1;";
        $result = mysql_query($query);
        $row = mysql_fetch_array($result);
        $fechaFin= $row['fecha'];
        $fechaIni = $this->fecha;    
        $dif = RestarHoras($fechaFin,$fechaIni);
        $this->duracion = $dif;*/
        $query_update=" update task set duracion=TIMEDIFF( now( ) ,fecha) where id='$this->id';";
        //$query_update=" update task set duracion='$this->duracion' where id='$this->id';";
        $result = mysql_query($query_update);
    }
    
    function toJson(){
        return  json_encode($this);
    }
    
    function getLast(){
        $query ="select * from task where username = '$this->username' order by fecha DESC limit 1;";
        $result = mysql_query($query);
        $row = mysql_fetch_array($result);
        $this->id = $row['id'];
        $this->fecha = $row['fecha'];
        $this->siguiente= $this;
        return $this;
    }
}
?>